
const fs = require('fs')
const csv = require('csv-parser')
const fastcsv = require('fast-csv');
const path = require('path')


function getPathByRule(ruleName){
  return path.join(__dirname, '../../../collect-app/data', ruleName + '.csv')
}

function getLogPath(){
  return path.join(__dirname, '../../../collect-app/data/log.csv')
}

function getJobPath(){
  return path.join(__dirname, '../../../collect-app/data/data_origin.csv')
}

exports.getJobPath = getJobPath

function getDetailPath(lid){
  return path.join(__dirname, '../../../collect-app/data/detail', lid + '.txt')
}

exports.getDetail = (req)=> {
  filepath = getDetailPath(req.lid)

  return new Promise((resolve, reject) => {
    fs.readFile(filepath, 'utf8', (err, data) => {
      if (err) {
        console.error('Error reading file:', err);
        resolve({ text: '没有找到数据' })
        return;
      }
      console.log('File content:', data);
      resolve({text : data})
    });
  })

}

exports.getLogData = () => {
  return new Promise((resolve, reject) => {
    const results = [];
    fs.createReadStream(getLogPath())
      .pipe(csv())
      .on('data', (data) => results.push(data))
      .on('end', () => {
        resolve(results)
      });
  })
}

exports.getJobList = () => {
  return new Promise((resolve, reject) => {
    const results = [];
    fs.createReadStream(getJobPath())
      .pipe(csv())
      .on('data', (data) => results.push(data))
      .on('end', () => {
        //获取详情信息
        const promiseList = []
        results.forEach(item => {
          const promiseObj = new Promise((resolve2, reject2) => {
            filepath = getDetailPath(item['编号'])
            fs.readFile(filepath, 'utf8', (err, data) => {
              if (err) {
                console.error('Error reading file: 没找到详情数据', err);
                resolve2();
                return;
              }
              // console.log('File content:', data);
              item.detail = data;
              resolve2();
            });
          })
          promiseList.push(promiseObj)

        })

        Promise.all(promiseList).then(() => {
          resolve(results)
        }).catch(() => {})

      });
  })
}

exports.updateSubmitStatus = ({ids, status}) => {
  const newRows = []

  return new Promise((resolve, reject) => {
    fs.createReadStream(getJobPath())
    .pipe(csv())
    .on('data', (row) => {
      if (ids.includes(row['编号'])) {
        row['进度'] = status
      }
      newRows.push(row)
    })
    .on('end', () => { 
      const csvStream = fastcsv.format({ headers: true });
      csvStream.pipe(fs.createWriteStream(getJobPath(),{
        flags: 'w', 
        encoding: 'utf8',
        autoClose: true
      }));
  
      newRows.forEach((row) => {
        csvStream.write(row);
      });
      
      csvStream.end();
      resolve( {data : newRows})
    })
  })

}

exports.updateJobCategory = ({lid, key, value, params}) => {
  const newRows = []

  return new Promise((resolve, reject) => {
    fs.createReadStream(getJobPath())
    .pipe(csv())
    .on('data', (row) => {
      if (lid === row['编号']) {
        if (params['category']) {
          row['类目'] = params['category']
        }
        if (params['group']) {
          row['状态'] = params['group']
        }
        if (params['targetGroup']) {
          row['targetGroup'] = params['targetGroup']
        }
        if (params['weight']) {
          row['weight'] = params['weight']
        }

        console.log('222222 ', row, params)
      }
      newRows.push(row)
    })
    .on('end', () => { 
      const csvStream = fastcsv.format({ headers: true });
      csvStream.pipe(fs.createWriteStream(getJobPath(),{
        flags: 'w', 
        encoding: 'utf8',
        autoClose: true
      }));
  
      newRows.forEach((row) => {
        csvStream.write(row);
      });
      
      csvStream.end();
      resolve( {data : newRows})
    })
  })

}

// exports.updateSubmitStatus({ids: ['6395a486cdd3fe25a1HB83d-0EFNQ'], status: '已投递'})


const salaryFilter = row => {
  const salaryDesc = row['薪资']
  const parts = (salaryDesc || '').split('-')
  if (parts.length < 2){
    return false
  }
  return parseInt(parts[0]) >= 10
}

const jobNameFilter = row => {
  if (row['职位'].includes('娱乐')){
    return false
  }

  return row['职位'].includes('主播')
}

function getRuleResult (ruleName) {
  return new Promise((resolve, reject) => {
    const filename = getPathByRule(ruleName)
    if (!fs.existsSync(filename)){
      resolve([])
      return
    }

    const results = []
    fs.createReadStream(filename)
    .pipe(csv())
    .on('data', (data) => {
      results.push(data)
    })
    .on('end', () => {
      resolve(results.filter(salaryFilter).filter(jobNameFilter))
    });

  })
}
exports.getRuleResult = getRuleResult

exports.runRule = (ruleName, args) => {
  console.log('222222 runRule', ruleName, args)

  const { PythonShell } = require('python-shell');
  const filename = getPathByRule(ruleName)
  if (args.clear && fs.existsSync(filename)){
    fs.unlinkSync(filename)
  }

  const scriptPath = path.resolve(__dirname, '../../../collect-app/collectors/scripts')
  const options = {
      mode: 'text',
      pythonPath: 'python3',
      scriptPath,
      args: [JSON.stringify(args)]
  };

  return new Promise((resolve, reject) => {
    const pyshell = new PythonShell(ruleName + '.py', options);
    console.log('run ', scriptPath, ruleName + '.py')

    pyshell.on('message', (message) => {
        console.log(`Python脚本消息: ${message}`);
    });
    pyshell.end((err,code,signal) => {
      if (err){
        console.error('Python脚本结束出错');
        resolve({res: 'fail', result: []})
        return
      }
      
      console.log('python脚本执行成功', code)
      getRuleResult(ruleName).then((result) => {
        resolve({res: 'success', result})
      })
    
    });
  })

}
